package com.starblink.android.guang.widget.cstnavbar

import android.graphics.Color
import com.starblink.android.basic.btmnavanim.BtmNavAnimControl
import com.starblink.android.basic.config.GuangConfig
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.string
import com.starblink.android.basic.ext.toastUI
import com.starblink.android.basic.ext.visible
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.guide.bean.SkGuideBusConfig
import com.starblink.android.basic.guide.bean.SkGuideEnum
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.android.guang.databinding.ActivityMainBinding
import com.starblink.basic.route.RoutePage
import com.starblink.basic.storage.SkStorage
import com.starblink.basic.util.CommUtils

/**
 * 底部导航自定义
 */
class PartNavBar(
    var viewBinding: ActivityMainBinding
) {

    var navBtmIsShow = true
    var dyAll: Long = 0
    val navBarHideH = CommUtils.getScreenHeight() / 2 //需求是1屏 因为recycleView上还有布局，将它算上

    var firstRocketAnim = false

    fun btmNavBarInit() {
        viewBinding.btmNavBlur.setOnClickListener {

        }
        //初始化数据
        CustomNavBarConfig.initNavBtm()
        //显示或隐藏底部导航监听
        BtmNavAnimControl.setScrollViewListener(object : BtmNavAnimControl.ScrollViewListener {
            //dy大于0 上拉
            //小于0 下拉
            override fun onScrollChanged(pageTag: String, dy: Int) {
                //小火箭的动画控制
                if (pageTag == "NAV_HOME_FIRST_TAG") {
                    if (dy > 0) {
                        dyAll += dy
                    } else if (dy < 0) {
                        dyAll += dy
                    }
//                    if (dyAll in 1 until navBarHideH) {
//                        if (!firstRocketAnim) {
//                            firstRocketAnim = true
//                            showRocket(true)
//                        }
//                    } else {
                    if (dy > 0 && navBtmIsShow) {
                        btmNavShowOrHide(false)
                    } else if (dy <= 0 && !navBtmIsShow) {
                        btmNavShowOrHide(true)
                    }
                    //滑动超出一单位 房子图标变火箭
                    if (dyAll > navBarHideH) {
                        showRocket(true)
                    } else {
                        showRocket(false)
                    }
//                    }
                } else {
                    //上滑 并且 正在显示底部栏
                    if (dy > 0 && navBtmIsShow) {
                        btmNavShowOrHide(false)
                    } else if (dy <= 0 && !navBtmIsShow) {
                        btmNavShowOrHide(true)
                    }
                }
            }
        })
        //加载navBar图片
        loadNavImgRes()
        //点击事件
        nvBtnClickEvent()
    }

    private fun showRocket(show: Boolean) {
        if (show) {
            if (!CustomNavBarConfig.mShowTopImg) {
                CustomNavBarConfig.mShowTopImg = true
                viewBinding.btmNavImg1Banner.viewPager2.setCurrentItem(1, true)
            }
        } else {
            if (CustomNavBarConfig.mShowTopImg) {
                CustomNavBarConfig.mShowTopImg = false
                viewBinding.btmNavImg1Banner.viewPager2.setCurrentItem(0, true)
            }
        }
    }

    private var adapter: FirstNavBtnAdapter? = null
    fun loadNavImgRes() {
        for ((index, resData) in CustomNavBarConfig.currentImgResData.withIndex()) {
            when (index) {
                0 -> {
                    if (resData.clicked) {
                        viewBinding.btmNavImg1Contain.setBackgroundColor(Color.parseColor("#E52E4C"))
                        if (resData.firstChoiceResArr.isNullOrEmpty()) {
                            viewBinding.btmNavImg1.visible()
                            viewBinding.btmNavImg1Banner.gone()
                            viewBinding.btmNavImg1.setImageResource(CustomNavBarConfig.btmNavImgFirstResNormal)
                        } else {
                            viewBinding.btmNavImg1.gone()
                            viewBinding.btmNavImg1Banner.visible()
                            if (adapter == null) {
                                adapter = FirstNavBtnAdapter(
                                    resData.firstChoiceResArr!!,
                                    viewBinding.btmNavImg1Contain.context
                                )
                                viewBinding.btmNavImg1Banner.setAdapter(adapter)
                            }
                            //禁止用户点击
                            viewBinding.btmNavImg1Banner.viewPager2.isUserInputEnabled = false
                        }
                    } else {
                        viewBinding.btmNavImg1Contain.setBackgroundColor(Color.parseColor("#00000000"))
                        viewBinding.btmNavImg1.visible()
                        viewBinding.btmNavImg1Banner.gone()
                    }
                }

                1 -> {
                    viewBinding.btmNavImg2.setImageResource(resData.normalRes)
                }

                2 -> {
                    viewBinding.btmNavImg3.setImageResource(resData.normalRes)
                }

                3 -> {
                    viewBinding.btmNavImg4.setImageResource(resData.normalRes)
                }
            }
        }

    }


    //    val btmNavHeight = 72.dp2px()
    fun btmNavShowOrHide(showOrHide: Boolean) {

        //产品最后说固定
        return

        // true 显示
        if (showOrHide) {
            navBtmIsShow = true
            //viewBinding.btmNavBlur.animate().translationY(0f)
            viewBinding.btmNavBlur.animate().scaleX(1f)
            viewBinding.btmNavBlur.animate().scaleY(1f)
            viewBinding.btmNavBlur.animate().alpha(1f)
        } else {
            navBtmIsShow = false
            //将Y属性变为底部栏高度 (相当于隐藏)
            //viewBinding.btmNavBlur.animate().translationY(btmNavHeight.toFloat())
            //改为缩放
            viewBinding.btmNavBlur.animate().scaleX(0f)
            viewBinding.btmNavBlur.animate().scaleY(0f)
            viewBinding.btmNavBlur.animate().alpha(0f)
        }
    }


    fun indirectGoFirstBtn() {
        //这里先后顺序必须这样写！！！
        setViewPagerIndex(0)
        //这里先后顺序必须这样写
        if (UserDataCenter.currentIndex != 0) {
            FlowBus.boolFlowBusNotify(FlowConst.HOME_GUIDE_NEXT)
        }
        //这里先后顺序必须这样写
        SpmTrackHandler.addSingleTrackData(
            SkAntEntity(
                spmCnt = GTrackerAssistUtils.fetchElementValue(
                    SpmPageDef.HomeRoute,
                    SpmElementDef.HomeTab
                ),
                params = mapOf(
                    "index" to "0",
                    "title" to string(com.starblink.android.guang.R.string.title_1)
                )
            )
        )
    }

    private fun nvBtnClickEvent() {
        viewBinding.btmNavImg1Cover.setOnClickListener {

            if (UserDataCenter.currentIndex != 0) {
                indirectGoFirstBtn()
            } else {
                //如果是飞机 点击滑到顶部
                if (CustomNavBarConfig.mShowTopImg) {
                    dyAll = 0
                    showRocket(false)
                    firstRocketAnim = false
                    //点击到顶部
                    FlowBus.boolFlowBusNotify(FlowConst.HOME_PAGE_SCROLL_TOP, true)
                }

            }
        }
        viewBinding.btmNavImg2.setOnClickListener {

            if (UserDataCenter.currentIndex == 1)
                return@setOnClickListener

            if (UserDataCenter.loadedCategory) {
                if (UserDataCenter.existCategory) {
                    setViewPagerIndex(1)
                    //收到触发一下search的收藏引导
                    FlowBus.boolFlowBusNotify(SkGuideBusConfig.HOME_FRG_1_PRODUCT_V2_NAME)
                    SpmTrackHandler.addSingleTrackData(
                        SkAntEntity(
                            spmCnt = GTrackerAssistUtils.fetchElementValue(
                                SpmPageDef.HomeRoute,
                                SpmElementDef.HomeTab
                            ),
                            params = mapOf(
                                "index" to "1",
                                "title" to string(com.starblink.android.guang.R.string.title_2)
                            )
                        )
                    )
//                            navigationTo(RoutePage.Search.PAGE_GOODS_CATEGORY)
                } else {
                    navigationTo(RoutePage.Search.PAGE_SEARCH) {}
                }
            }
        }

        viewBinding.btmNavImg3.trackData(
            page = SpmPageDef.HomeRoute,
            element = SpmElementDef.HomeTab,
            onlyHit = true,
            params = mapOf(
                "title" to "message"
            )
        )
        viewBinding.btmNavImg3.setOnClickListener {

            if (UserDataCenter.currentIndex == 2)
                return@setOnClickListener

            setViewPagerIndex(2)
            /*val ifSpm = SkStorage.getBoolean(UserDataCenter.HOME_TAB_INDEX_NOSPM, false)
            if (!ifSpm) {
                //false-上报
                SpmTrackHandler.addSingleTrackData(
                    SkAntEntity(
                        spmCnt = GTrackerAssistUtils.fetchElementValue(
                            SpmPageDef.HomeRoute,
                            SpmElementDef.HomeTab
                        ),
                        params = mapOf(
                            "index" to "2",
                            "title" to string(com.starblink.android.guang.R.string.title_4)
                        )
                    )
                )
            }*/

            //心愿单位置变了，下边是否要变更逻辑 12023-10-10

            //回执状态
            SkStorage.put(UserDataCenter.HOME_TAB_INDEX_NOSPM, false)
//            wishListFragment.refreshAllItems()
        }

        viewBinding.btmNavImg4.setOnClickListener {
            if (UserDataCenter.currentIndex == 3)
                return@setOnClickListener
            setViewPagerIndex(3)
            SpmTrackHandler.addSingleTrackData(
                SkAntEntity(
                    spmCnt = GTrackerAssistUtils.fetchElementValue(
                        SpmPageDef.HomeRoute,
                        SpmElementDef.HomeTab
                    ),
                    params = mapOf(
                        "index" to "3",
                        "title" to string(com.starblink.android.guang.R.string.title_5)
                    )
                )
            )
        }

    }

    private fun setViewPagerIndex(index: Int) {
        viewBinding.viewpager.setCurrentItem(index, false)
        UserDataCenter.currentIndex = index
        CustomNavBarConfig.setNavBtmWhoBeChoice(index)
        loadNavImgRes()
    }

}